#include <stdio.h>
#include "crc16.h"

/* 计算模型 CRC-16/MODBUS
 * 参考链接：http://www.ip33.com/crc.html
*/
uint16_t CRC_Compute(uint8_t *puchMsg, uint16_t usDataLen) 
{ 
	uint16_t crc16 = 0xffff;

	for (int i = 0; i < usDataLen; i++)
	{
			crc16 ^= puchMsg[i];

			for (int j = 0; j < 8; j++)
			{
					if ((crc16 & 0x01) == 1)
					{
							crc16 = (crc16 >> 1) ^ 0xa001;
					}
					else
					{
							crc16 = crc16 >> 1;
					}
			}
	}

	return crc16 ;
}						



uint8_t CheckSum(const uint8_t* data, uint16_t len) {
  uint8_t crc = 0;
  for (uint16_t i = 0; i < len; ++i) {
    crc += data[i];
  }
  return crc;
}

